import random
import threading
import time

# 声明变量记录最终的名次
ranking = []

# 声明变量，存储比赛的距离
distance = 100


# 兔子线程类
class RabbitThread(threading.Thread):
    """
    每跑5米就会随机的休息1到10秒
    """

    def run(self):
        t = threading.current_thread()
        print("当前执行的线程是{}".format(t.name))
        start = time.time()  # 开始时间
        for i in range(distance):
            if i % 5 == 0 and i > 0:
                sleep_time = random.randint(1, 10)
                # print("乌龟已经跑了{}米，需要休息{}秒".format(i,sleep_time))
                time.sleep(sleep_time)
        ranking.append(t.name)
        end = time.time()  # 结束时间
        print("线程{}总执行时间:{}".format(t.name, end - start))


# 乌龟线程类
class TortoiseThread(threading.Thread):
    """
    乌龟线程：乌龟仍然也是一如既往的坚持，每秒钟跑1米。
    """

    def run(self):
        t = threading.current_thread()
        print("当前执行的线程是{}".format(t.name))
        start = time.time()  # 开始时间
        for i in range(distance):
            time.sleep(1)
        ranking.append(t.name)
        end = time.time()  # 结束时间
        print("线程{}总执行时间:{}".format(t.name, end - start))


# 大象线程类
class ElephantThread(threading.Thread):
    """
    大象线程:大象每跑10米，会随机休息1到15秒
    """

    def run(self):
        t = threading.current_thread()
        print("当前执行的线程是{}".format(t.name))
        start = time.time()  # 开始时间
        for i in range(distance):
            if i > 0 and i % 10 == 0:
                time.sleep(random.randint(1, 15))
        ranking.append(t.name)
        end = time.time()  # 结束时间
        print("线程{}总执行时间:{}".format(t.name, end - start))


# 狼线程类
class WolfThread(threading.Thread):
    """
    狼和老虎都是每跑15米会休息1到20秒
    """

    def run(self):
        t = threading.current_thread()
        print("当前执行的线程是{}".format(t.name))
        start = time.time()  # 开始时间
        for i in range(distance):
            if i > 0 and i % 15 == 0:
                time.sleep(random.randint(1, 20))
        ranking.append(t.name)
        end = time.time()  # 结束时间
        print("线程{}总执行时间:{}".format(t.name, end - start))


# 老虎线程类
class TigerThread(threading.Thread):
    """
    狼和老虎都是每跑15米会休息1到20秒
    """

    def run(self):
        t = threading.current_thread()
        print("当前执行的线程是{}".format(t.name))
        start = time.time()  # 开始时间
        for i in range(distance):
            if i > 0 and i % 15 == 0:
                time.sleep(random.randint(1, 20))
        ranking.append(t.name)
        end = time.time()  # 结束时间
        print("线程{}总执行时间:{}".format(t.name, end - start))


# 熊线程类
class BearThread(threading.Thread):
    """
    熊每跑3米会休息1到5秒
    """

    def run(self):
        t = threading.current_thread()
        print("当前执行的线程是{}".format(t.name))
        start = time.time()  # 开始时间
        for i in range(distance):
            if i > 0 and i % 3 == 0:
                time.sleep(random.randint(1, 5))
        ranking.append(t.name)
        end = time.time()  # 结束时间
        print("线程{}总执行时间:{}".format(t.name, end - start))


# 实例化每一个动物线程对象
rabbit_thread = RabbitThread(name="rabbit")
tortoise_thread = TortoiseThread(name="tortoise")
elephant_thread = ElephantThread(name="elephant")
wolf_thread = WolfThread(name="wolf")
tiger_thread = TigerThread(name="tiger")
bear_thread = BearThread(name="bear")

# 让动物们开始跑起来
start = time.time()
print("运动会开始了，现在的时间是", start)
rabbit_thread.start()
tortoise_thread.start()
elephant_thread.start()
wolf_thread.start()
tiger_thread.start()
bear_thread.start()

# 设置线程加入等待，主线程必须等待他们执行完毕后才继续向下执行

rabbit_thread.join()
tortoise_thread.join()
elephant_thread.join()
wolf_thread.join()
tiger_thread.join()
bear_thread.join()
end = time.time()
print("本次比赛的结果是" + str(ranking))
print("本次比赛的结束时间是：{}，共耗时：{}".format(end, end - start))
